package nbcp.db.mongo.entity

import nbcp.base.comm.WxUserInfoRef
import nbcp.base.extend.AsLocalDateTime
import nbcp.db.*
import nbcp.db.mongo.*
import org.springframework.data.mongodb.core.mapping.Document
import java.time.LocalDateTime

/**
 * Created by udi on 17-3-28.
 */
//
//data class ProductInfo_Sku(
//        var key: String = "",
//        var values: MutableList<String> = mutableListOf()
//) {}




@Document
@MongoEntityGroup("info")
data class ProductInfo(
        var name: String = "",
        var slogan: String = "", //口号。少于16个字
//        var type: String = "",
        var category: IdCodeName = IdCodeName(),
        var barCode: String = "", //条形码
        var parameters: MutableList<KeyValueString> = mutableListOf(),

//        var logos: MutableList<IdUrl> = mutableListOf(), //正方形Url
//        var adImage: IdUrl = IdUrl(),                   //16:9比例的广告牌图片
        var showImages: MutableList<IdUrl> = mutableListOf(),  //轮播图片

        var images: MutableList<IdUrl> = mutableListOf(), //产品详情图片
        var status: ProductStatusEnum = ProductStatusEnum.Online,

        var video: IdUrl = IdUrl(),

        var detail: String = "", //商品详情前面的文字 .
        var remark: String = "", //商品详情最后的文字 .

        var corp: IdName = IdName(),
        var guidePrice: Int = 0,    //官方价格：分
        var length: Int = 0,        //单位: mm
        var width1: Int = 0,        //单位: mm
        var width2: Int = 0,        //单位: mm
        var height: Int = 0,        // 单位: mm
        var thick: Int = 0 ,        //厚度，单位:mm
        var createAt: LocalDateTime = LocalDateTime.now(),

        //sku
        var skuDefines: MutableList<KeyValueString> = mutableListOf(),
        var skuStockPrice: MutableList<ProductSkuStockPrice> = mutableListOf(),

        var material: String = "", // 材料
//        var code: String = "", //编号
//        var activity: ActivityRef = ActivityRef(), //有效活动
//        var activityStock: Int = 0, //活动商品使用数量
//        var stock: Int = 0,         //活动商品使用数量,以后从 sku中计算

        var saleCount: Int = 0,         //销量,支付成功后更新。
        var freight: Int = 0  //运费

) : IMongoDocument() {
    val logo:IdUrl
        get() = this.showImages.firstOrNull() ?: IdUrl()
//    val activityStatus: ActivityStatusEnum
//        get() {
//            if (this.activityType == ActivityTypeEnum.Empty) {
//                return ActivityStatusEnum.None;
//            }
//
//            if (this.activityNotice == null) {
//                return ActivityStatusEnum.Going;
//            }
//
//            var now = LocalDateTime.now();
//            if (this.activityNotice!!.startAt < now) {
//                return ActivityStatusEnum.NotStarted;
//            }
//            if (this.activityNotice!!.endAt > now) {
//                return ActivityStatusEnum.Over;
//            }
//
//            return ActivityStatusEnum.Going;
//        }

    //普通销售的最低价格.
//    val salePrice: Int
//        get() {
//            return this.skuStockPrice.filter { it.stock > 0 && it.salePrice >= 0 }.map { it.salePrice }.min()
//                    ?: this.guidePrice;
//        }

    //最低价格(含活动)
//    val minPrice: Int
//        get() {
//            var min = this.skuStockPrice.filter { it.stock > 0 && it.salePrice >= 0 }.map { it.salePrice }.min()
//                    ?: this.guidePrice;
//
//
//            return min;
//        }

    //最高价格(含活动)
//    val maxPrice: Int
//        get() {
//            var max = this.skuStockPrice.filter { it.stock > 0 }.map { it.salePrice }.max() ?: this.guidePrice;
//
//            return max;
//        }

    //所有Sku的总库存量.
//    val stock: Int
//        get() {
//            return this.skuStockPrice.map { it.stock }.sum();
//        }
}

//仓库信息
@Document
@MongoEntityGroup("info")
data class StockInfo(
        var name: String = "",
        var city: IntCodeName = IntCodeName(),
        var address: String = "",
        var remark: String = "",
        var createAt: LocalDateTime = LocalDateTime.now(),
        var createBy: IdName = IdName(),
        var corp: IdName = IdName()
) : IMongoDocument()


//经销商经手的商品,以及生产商录码的商品（不包含生产商发布的未录码的商品）。
//@Document
//data class SalerAllProductInfo(
//        var product: IdName = IdName(),
//        var corp: IdName = IdName()
//): IMongoDocument()

@Document
@MongoEntityGroup("info")
open class OrderInfo(
        var orderCode: String = "", //订单号

        var status: OrderStatusEnum = OrderStatusEnum.Created,
        var createAt: LocalDateTime = LocalDateTime.now(),
        var createBy: WxUserInfoRef = WxUserInfoRef(),
        var corp: IdName = IdName(),

        var receiveAddress: SysCorporation_ReceiveAddress = SysCorporation_ReceiveAddress(),

        //物流信息
        var express: IdName = IdName(), //快递公司
        var expressNumber: String = "", //快递单号

        var details: MutableList<OrderInfo_ProductItem> = mutableListOf(), //商品
        var payInfo: OrderInfo_PayInfo = OrderInfo_PayInfo(),
        var amount: Int = 0, //应付金额.
        var amount_discount: Int = 0, //商家给的折扣.

//        var activityId: String = "",            //Activity.id
        var operations: MutableList<OrderInfo_OperationItem> = mutableListOf(), //操作记录

        var headerOrderCode: String = "", // 团长订单Code ,拆单前的总订单号.
        var groupOrderCount: Int = 0,    // 小组订单数.

        var sentAt: LocalDateTime?, //发货时间
        var evaluate: String = "", //评价
        var receiveAt: LocalDateTime?, //收货时间
        var remark: String = ""   //消费者填写说明。
) : IMongoDocument() {

    //是否可以发货.
    val canDeliver: Boolean
        get() {
            //普通订单,已支付.
            return this.status == OrderStatusEnum.Payed;
        }

    companion object {
    }
}

//退款订单信息
@Document
@MongoEntityGroup("info")
open class RefundInfo(
        var orderCode: String = "", //订单号
        var refundCode: String = "",  //退款订单号
        var createAt: LocalDateTime = LocalDateTime.now(),
        var orderAmount: Int = 0,    //订单总金额
        var refundAmount: Int = 0,    //退款金额
        var reason: String = "",      //退款原因
        var status: Boolean = false,  // 退款状态 , true 退款成功
        var sysCode: String = "",     //系统消息code
        var sysMessage: String = ""       //系统消息
) : IMongoDocument() {


    fun toWxRefoundDataXml(wxConfig: WxShopConfig): String {
//        //TODO   yml 文件不能读取参数识别
//        wxConfig.appId = "wx5f7e51f3baeefee3"
//        wxConfig.appSecret ="1234"
//        wxConfig.mchId = "1234"
        var ret = WxRefundParameterData(
                wxConfig.appId,
                wxConfig.appSecret,
                wxConfig.mchId,
                this.orderCode,
                this.refundCode,
                this.orderAmount,
                this.refundAmount,
                "",
                this.reason,
                "",
                "");
        return ret.toXml();
    }
}

//物流公司
@Document
@MongoEntityGroup("info")
data class ExpressInfo(
        var createAt: LocalDateTime = LocalDateTime.now(),
        var createBy: IdName = IdName(),
        var corp: IdName = IdName(),

        var site: String = "",  //查询网址
        var name: String = "", //快递公司
        var remark: String = "" //快递备注
) : IMongoDocument()


////混合装箱商品
//@Document
//data class MixBoxProduct(
//        var name: String = "",
//        var createAt: LocalDateTime = LocalDateTime.now(),
//        var corp: IdName = IdName(),
//        var specifications: MutableList<MixBoxProduct_Specification> = mutableListOf() //规格
//): IMongoDocument()


//申请人信息
@Document
@MongoEntityGroup("info")
data class ApplyInfo(
        var name: String = "",
        var mobile: String = "",
        var corpName: String = "",
        var trade: String = ""
) : IMongoDocument()